package com.action;

import com.pojo.User;
import com.service.QuestionService;
import com.service.RegisterService;
import com.service.UserService;
import com.token.JwtUtil;
import com.util.MD5Util;
import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * User: Qsxmj
 * Date: 2019/10/9 16:02
 * Content:
 *
 * @author qsxmji
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private UserService userService;
    @Autowired
    private RegisterService registerService;

    @Autowired
    private QuestionService questionService;

    /**
     *  根据试卷id查询试卷中的题目
     * @param username  用户账号  用于缓存的key
     * @param testId  试卷id
     * @return
     */
    @RequestMapping("/queryTest")
    public ResponseBean queryTest(String username , Integer testId){
        // 根据试卷id查询试卷中的题目
        Map map= questionService.queryTest(username,testId);
        if(map!=null){
            return new ResponseBean(200,0,map);
        }
        return new ResponseBean(500,0,null);
    }


    /**
     * @param req        用户请求
     * @param oldPassword 原密码
     * @param newPassword 新密码
     * @return 首先验证原密码是否与数据库中的密码相同，如果相同，则修改
     */

    @RequestMapping("/updatePassword")
    public ResponseBean updatePassword(String oldPassword, String newPassword,  HttpServletRequest req){
        String username = JwtUtil.getUsername(req.getHeader("token"));
        String user_password=userService.checkPassword(username);
        String userOldPassword=MD5Util.string2MD5(oldPassword);
        String userNewPassword=MD5Util.string2MD5(newPassword);
        if(user_password!=null&user_password.equals(userOldPassword)){
            int n=userService.updatePasswordByName(username,userNewPassword);
            if(n>0){
                return new ResponseBean(200,0,"修改成功");
            }
        }
        return new ResponseBean(500,0,"修改失败");
    }
    /**
     *
     * @param username  接收界面上的用户名
     * @param mail      接收界面上的邮箱
     * @param password  接收界面上的密码
     * @return  1.用户表添加数据  2.学生表添加用户名
     */

    @RequestMapping("/addUser")
    public ResponseBean addUser(String username,String mail,String password) {
        int x = registerService.addUser(username,mail,password);
        if (x > 0) {
            return new ResponseBean(200, 0, "注册成功");
        }
        return new ResponseBean(500, 0, "注册失败");
    }

    // TODO: 2019/12/6  
    //测试用登陆
    @PostMapping("/login")
    public ResponseBean login(String username, String password, HttpServletResponse resp){

        User user = userService.login(username,password);
        if(user!=null){
            loginSuccess(user,resp);
            return new ResponseBean(200,0,user);
        }
        return new ResponseBean(500,0,"登陆失败");
    }


    /**
     * 登录成功后更新token
     *
     */
    private void loginSuccess(User user, HttpServletResponse resp) {
        resp.setHeader("Access-Control-Expose-Headers", "token");
        String refreshToken = JwtUtil.generToken(user.getUserName(), "admin", "UserInfo");
        resp.setHeader("token", refreshToken);
    }
    /**
     * 发送邮件
     * @param mail  目的邮箱
     * @return
     */
    @RequestMapping("/sendMail")
    public String sendMail(String mail)
    {
        registerService.sendMail(mail);
        return "success";
    }
    /**
     *
     * @param username 接收界面上的用户名
     * @return   查询数据库已存在的用户名
     */
    @PostMapping("/findUser")
    public ResponseBean findUser(String username){
        return new ResponseBean(200,0,registerService.findUser(username));
    }

    /**
     *
     * @param mail  接收界面上的邮箱
     * @return    查询数据库已存在的邮箱
     */
    @PostMapping("/findMail")
    public ResponseBean findMail(String mail){
        return new ResponseBean(200,0,registerService.findMail(mail));
    }
}
